$(document).ready(function(){
	$("#tab_ap_button").click(refresh_ap);
	
	$("#ap_indv_setpoints input").bind('change',function(){
		$(this).animate({backgroundColor: "#0000FF" }, 500);//temporary blue to notify the user of the change
	});
	$("#ap_indv_settings input").click(function(){
		var full = $(this).attr("id").replace("ap_","");
		var parts = full.split("_");
		var name = parts[0];
		var mode = parts[1];
		var val;
		if (mode == "off"){val = 0;}
		else if (mode == "on"){val = 1;}
		else if (mode == "roll"){val = 1;}
		else if (mode == "heading"){val = 2;}
		else if (mode == "pitch"){val = 1;}
		else if (mode == "vrate"){val = 2;}
		else if (mode == "alt"){val = 3;}
		else{return;}
		$.ajax({
			url: "http://localhost:8080/at_changeIndvMode?mode="+name+"&val="+val,
			beforeSend: error_ap,
			success: refresh_ap
		});
	});
	$("#ap_mode input").click(function(){
		var name = $(this).attr("id").replace("ap_mode_","");
		var val;
		if (name == "standby"){val=0;}
		else if (name == "wp"){val=1;}
		else if (name == "indv"){val=2;}
		else if (name == "land"){val=3;}
		else{return;}
		$.ajax("http://localhost:8080/at_changeMode?mode="+val)
			.success(refresh_ap);
	});
	$("#ap_setpoint_save").click(function(){
		var roll = $("#ap_setpoint_roll").val();
		var heading = $("#ap_setpoint_heading").val();
		var pitch = $("#ap_setpoint_pitch").val();
		var vrate = $("#ap_setpoint_vrate").val();
		var alt = $("#ap_setpoint_alt").val();
		var thr = $("#ap_setpoint_throttle").val();
		var slip = $("#ap_setpoint_slip").val();
		$.ajax({
			url: "http://localhost:8080/at_changeSetpoints?roll="+roll+"&heading="+heading+"&pitch="+pitch+"&vrate="+vrate+"&altitude="+alt+"&throttle="+thr+"&slip="+slip,
			beforeSend: error_ap,
			success: refresh_ap
		});
	});
	
	error_ap();
	
	function error_ap()
	{
		$("#ap_mode input").each(function(index){
			$(this).attr("disabled",true).button("refresh");
		});
		$("#ap_indv_settings input").each(function(index){
			$(this).attr("disabled",true).button("refresh");
		});
		$("#ap_indv_setpoints input").each(function(index){
			$(this).attr("disabled",true).button("refresh");
			$(this).animate({backgroundColor: "#000000" }, 500);
		});
		$("#ap_setpoint_save").attr("disabled",true).button("refresh");
	}
	
	function refresh_ap()
	{
		//alert("GOGOGOGOGOGOGO");
		$.ajax({
			type: "GET",
			url: "http://localhost:8080/at_getAP",
			dataType: "xml",
			beforeSend: error_ap,
			success: function(xml) {
				if ($(xml).find("error").text() != "False"){return;}//do not continue with an error
				switch (parseInt($(xml).find("mode").text()))
				{
					case 0:$("#ap_mode_standby").attr("checked",true);break;
					case 1:$("#ap_mode_wp").attr("checked",true);break;
					case 2:$("#ap_mode_indv").attr("checked",true);break;
					case 3:$("#ap_mode_land").attr("checked",true);break;
					default:
						alert("ERROR WITH MAIN MODE "+$(xml).find("mode").text());
						break;//for now
				}
				var indv = $(xml).find("indv");
				var indv_roll = parseInt(indv.find("roll").text());
				var indv_pitch = parseInt(indv.find("pitch").text());
				var indv_thro = indv.find("throttle").text()=="True";
				var indv_slip = indv.find("slip").text()=="True";
				switch (indv_roll)
				{
					case 0:$("#ap_roll_off").attr("checked",true);break;
					case 1:$("#ap_roll_roll").attr("checked",true);break;
					case 2:$("#ap_roll_heading").attr("checked",true);break;
					default:break;//TODO: Display an error here?
				}
				switch (indv_pitch)
				{
					case 0:$("#ap_pitch_off").attr("checked",true);break;
					case 1:$("#ap_pitch_pitch").attr("checked",true);break;
					case 2:$("#ap_pitch_vrate").attr("checked",true);break;
					case 3:$("#ap_pitch_alt").attr("checked",true);break;
					default:break;//TODO: Display an error here?
				}
				if (indv_thro)
				{$("#ap_throttle_on").attr("checked",true);}
				else {$("#ap_throttle_off").attr("checked",true);}
				if (indv_slip)
				{$("#ap_slip_on").attr("checked",true);}
				else {$("#ap_slip_off").attr("checked",true);}
				
				var sp = $(xml).find("setpoints");
				$("#ap_indv_setpoints input").each(function(){
					var name = $(this).attr("id").replace("ap_setpoint_","");
					$(this).val(sp.find(name).text());
				});
				
				//Enable everything
				$("#ap_mode input").each(function(index){
					$(this).removeAttr("disabled").button("refresh");
				});
				$("#ap_indv_settings input").each(function(index){
					$(this).removeAttr("disabled").button("refresh");
				});
				$("#ap_indv_setpoints input").each(function(index){
					var name = $(this).attr("id").replace("ap_setpoint_","");
					var usable = true;
					if (name == "roll"){usable = indv_roll<2;}
					if (name == "pitch"){usable = indv_pitch<2;}
					else if (name == "vrate"){usable = indv_pitch<3;}
					if (usable)
					{
						$(this).removeAttr("disabled").button("refresh");
						$(this).animate({backgroundColor: "#FFFFFF" }, 500);
					}
				});
				$("#ap_setpoint_save").removeAttr("disabled").button("refresh");
			}
		});
	}
});